package com.novel.web.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.novel.base.entity.R;
import com.novel.web.entity.NfBookshelf;
import com.novel.web.service.NfBookshelfService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 书架表
 *
 * @author 樊温军
 * @date Fri May 09 15:15:23 CST 2025
 */
@Slf4j
@Api(tags = "书架表", value = "书架表")
@RestController
@AllArgsConstructor
@RequestMapping("/nfBookshelf")
public class NfBookshelfController {

    private final NfBookshelfService nfBookshelfService;

    /**
     * 书架表—列表
     *
     * @param nfBookshelf 书架表
     * @return R<List < NfBookshelf>>
     */
    @ApiOperation(value = "书架表—列表", notes = "书架表—列表")
    @GetMapping("/list")
    public R<List<NfBookshelf>> getNfBookshelfList(NfBookshelf nfBookshelf) {
        log.info("开始查询书架表—列表，参数：{}", nfBookshelf);
        try {
            return R.ok(nfBookshelfService.getNfBookshelfList(nfBookshelf), "查询成功");
        } catch (Exception e) {
            log.error("查询书架表—列表时发生错误：{}", e.getMessage(), e);
            return R.failed("查询书架表—列表时发生错误，请稍后重试！");
        }
    }

    /**
     * 书架表—列表—分页
     *
     * @param page        分页对象
     * @param nfBookshelf 书架表
     * @return R<IPage < NfBookshelf>>
     */
    @ApiOperation(value = "书架表—列表—分页", notes = "书架表—列表—分页")
    @GetMapping(value = "/page")
    public R<IPage<NfBookshelf>> getNfBookshelfPage(Page<NfBookshelf> page, NfBookshelf nfBookshelf) {
        log.info("开始查询书架表—列表—分页，第 {} 页，每页 {} 条记录，其他参数：{}", page.getCurrent(), page.getSize(), nfBookshelf);
        try {
            IPage<NfBookshelf> iPage = nfBookshelfService.getPage(page, nfBookshelf);
            return R.ok(iPage, "查询成功");
        } catch (Exception e) {
            log.error("查询书架表—列表—分页时发生错误：{}", e.getMessage(), e);
            return R.failed("查询书架表—列表—分页时发生错误，请稍后重试！");
        }
    }

    /**
     * 书架表—保存
     *
     * @param nfBookshelf 书架表
     * @return R<Long>
     */
    @ApiOperation(value = "书架表—保存", notes = "书架表—保存")
    @PostMapping(value = "/saveOne")
    public R<Long> saveOneNfBookshelf(@Valid @RequestBody NfBookshelf nfBookshelf) {
        log.info("开始保存书架表：{}", nfBookshelf);
        try {
            Long id = nfBookshelfService.saveOneNfBookshelf(nfBookshelf);
            log.info("书架表保存成功，ID: {}", id);
            return R.ok(id, "保存成功");
        } catch (Exception e) {
            log.error("保存书架表时发生错误：{}", e.getMessage(), e);
            return R.failed("保存书架表时发生错误，请稍后重试！");
        }
    }

    /**
     * 书架表—删除
     *
     * @param id 书架表主键
     * @return R<Boolean>
     */
    @ApiOperation(value = "书架表—删除", notes = "书架表—删除")
    @PostMapping(value = "/delById")
    public R<Boolean> delByIdNfBookshelf(Long id) {
        log.info("开始删除书架表：{}", id);
        if (id == null || id <= 0) {
            log.warn("ID为空或无效: {}", id);
            return R.failed("请传入有效的ID！");
        }
        try {
            nfBookshelfService.delById(id);
            return R.ok(Boolean.TRUE, "删除成功");
        } catch (Exception e) {
            log.error("删除书架表失败: ID={}, Error={}", id, e.getMessage(), e);
            return R.failed("删除失败，请稍后重试！");
        }
    }

    /**
     * 书架表—详情
     *
     * @param id 书架表主键
     * @return R<NfBookshelf>
     */
    @ApiOperation(value = "书架表—详情", notes = "书架表—详情")
    @GetMapping(value = "/details")
    public R<NfBookshelf> getNfBookshelf(Long id) {
        log.info("开始查询书架表：{}", id);
        if (id == null || id <= 0) {
            log.warn("ID为空或无效: {}", id);
            return R.failed("请传入有效的ID！");
        }
        try {
            NfBookshelf nfBookshelf = nfBookshelfService.queryById(id);
            if (nfBookshelf != null) {
                return R.ok(nfBookshelf, "查询成功");
            } else {
                return R.failed("未找到书架表");
            }
        } catch (Exception e) {
            log.error("查询书架表详情时发生错误：{}", e.getMessage(), e);
            return R.failed("查询书架表详情时发生错误，请稍后重试！");
        }
    }
}
